package com.joker.orange.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.helper.LoginHelper;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.AgentUserBo;
import com.joker.orange.domain.vo.AgentUserVo;
import com.joker.orange.domain.AgentUser;
import com.joker.orange.mapper.AgentUserMapper;
import com.joker.orange.service.IAgentUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 代理信息Service业务层处理
 *
 * @author Joker
 * @date 2024-08-04
 */
@RequiredArgsConstructor
@Service
public class AgentUserServiceImpl implements IAgentUserService {

    private final AgentUserMapper baseMapper;

    /**
     * 查询代理信息
     */
    @Override
    public AgentUserVo queryById(Long userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询代理信息列表
     */
    @Override
    public TableDataInfo<AgentUserVo> queryPageList(AgentUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AgentUser> lqw = buildQueryWrapper(bo);
        Page<AgentUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for (AgentUserVo record : result.getRecords()) {
            AgentUserVo agentUserVo = baseMapper.selectVoOne(new LambdaQueryWrapper<AgentUser>()
                .eq(AgentUser::getUserId, record.getSuperiorAgent()));
            record.setSuperiorAgentName(agentUserVo.getNickName());
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询代理信息列表
     */
    @Override
    public List<AgentUserVo> queryList(AgentUserBo bo) {
        LambdaQueryWrapper<AgentUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AgentUser> buildQueryWrapper(AgentUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AgentUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, AgentUser::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), AgentUser::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), AgentUser::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getUserType()), AgentUser::getUserType, bo.getUserType());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), AgentUser::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), AgentUser::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), AgentUser::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), AgentUser::getAvatar, bo.getAvatar());
        lqw.ne(AgentUser::getAgentLevel, "0");
        lqw.ne(AgentUser::getUserId, LoginHelper.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getSuperiorAgent()), AgentUser::getSuperiorAgent, bo.getSuperiorAgent());
        lqw.eq(StringUtils.isNotBlank(bo.getRegistrationDeductionPermission()), AgentUser::getRegistrationDeductionPermission, bo.getRegistrationDeductionPermission());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderDeductionPermission()), AgentUser::getOrderDeductionPermission, bo.getOrderDeductionPermission());
        lqw.eq(bo.getCommissionRatio() != null, AgentUser::getCommissionRatio, bo.getCommissionRatio());
        lqw.eq(bo.getAndroidRegistrationPrice() != null, AgentUser::getAndroidRegistrationPrice, bo.getAndroidRegistrationPrice());
        lqw.eq(bo.getIosRegistrationPrice() != null, AgentUser::getIosRegistrationPrice, bo.getIosRegistrationPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getWithdrawCode()), AgentUser::getWithdrawCode, bo.getWithdrawCode());
        lqw.eq(bo.getBalance() != null, AgentUser::getBalance, bo.getBalance());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), AgentUser::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getWithdrawPassword()), AgentUser::getWithdrawPassword, bo.getWithdrawPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getIsAgent()), AgentUser::getIsAgent, bo.getIsAgent());
        lqw.eq(StringUtils.isNotBlank(bo.getInviteCode()), AgentUser::getInviteCode, bo.getInviteCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AgentUser::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginIp()), AgentUser::getLoginIp, bo.getLoginIp());
        lqw.eq(AgentUser::getCreateBy, LoginHelper.getUsername());
        lqw.eq(bo.getLoginDate() != null, AgentUser::getLoginDate, bo.getLoginDate());
        return lqw;
    }

    /**
     * 新增代理信息
     */
    @Override
    public Long insertByBo(AgentUserBo bo) {
        AgentUser add = BeanUtil.toBean(bo, AgentUser.class);
        add.setSuperiorAgent(String.valueOf(LoginHelper.getUserId()));
        add.setPassword(BCrypt.hashpw(add.getPassword()));
        add.setWithdrawPassword(BCrypt.hashpw(add.getPassword()));
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return add.getUserId();
    }

    /**
     * 修改代理信息
     */
    @Override
    public Boolean updateByBo(AgentUserBo bo) {
        AgentUser update = BeanUtil.toBean(bo, AgentUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AgentUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除代理信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
